package com.shengyuan.manage_os.sqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.util.Base64;

import com.shengyuan.manage_os.bean.UserInfo;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

import oa.database.GreenDaoHelper;
import oa.database.bean.Department;
import oa.database.bean.Employee;
import oa.database.bean.Friends;
import oa.database.dao.EmployeeDao;
import oa.database.dao.FriendsDao;

/**
 * Created by Administrator on 2016/11/22.
 */

public class SQLiteTools {
    private SQLiteDatabase database;
    private DBOpenHelp dbOpenHelp;
    private Context context;
    private static List<Friends> friends = new ArrayList<>();
    private static List<Department> departments = new ArrayList<>();
    private static List<Employee> employees = new ArrayList<>();
    private static Employee employee;

    public SQLiteTools(Context context) {
        dbOpenHelp = new DBOpenHelp(context);
    }
    public String[][] query(String table,String[] columnName,Bitmap bitmap){
        database = dbOpenHelp.getReadableDatabase();
        Cursor cursor = database.query(table,columnName,null,null,null,null,null);
        String[][] aa = new String[cursor.getCount()][3];
        int i = 0;
        while (cursor.moveToNext()) {
            for (int r = 0; r < columnName.length; r++){

                String values = cursor.getString(cursor.getColumnIndex(columnName[r]));
                aa[i][r] = values;
            }
            if (bitmap != null) {
                byte[] hear_image = cursor.getBlob(cursor.getColumnIndex("hear_image"));//取出图片
            }
            i++;

        }
        cursor.close();
        database.close();
        return aa;
    }
    public void add(String table,String[] name ,String[] valus, Bitmap bmp){
        database = dbOpenHelp.getWritableDatabase();
        ContentValues values = new ContentValues();
        for (int i= 0; i<name.length; i++) {
            values.put(name[i], valus[i]);
        }
        if(bmp != null){

            final ByteArrayOutputStream os = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 100, os);
            values.put("hear_image", os.toByteArray());
        }

        database.insert(table,null, values);
        database.close();
    }

    public void delete(String table,String id){
        database = dbOpenHelp.getWritableDatabase();
        database.delete(table,"id=?",new String[]{id});
        database.close();
    }

    public void updata(String table,String upID, String[] name, String[] values,Bitmap bitmap){
        database = dbOpenHelp.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        for (int i = 0; i < name.length; i++){
            contentValues.put(name[i],values[i]);
        }
        if(bitmap != null){

            final ByteArrayOutputStream os = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
            contentValues.put("hear_image", os.toByteArray());
        }
        database.update(table,contentValues,"id=?",new String[]{upID});
        database.close();
    }

    public void addFriend(long fid) {
        Friends friends = new Friends();
//        friends.setUid(UserInfo.UID);
        friends.setFid(fid);
        GreenDaoHelper.getInstance().singleInsert(friends);

    }

    public static void addEmployeeS(List<Employee> list) {

        GreenDaoHelper.getInstance().multiInsert(list);
    }

    public static void addDepartment(long pid, String name, long mid, String mname) {
        Department department = new Department(pid, name, mid, mname);
        GreenDaoHelper.getInstance().singleInsert(department);
    }
    public static void addDepartmentS(List<Department> list) {
        GreenDaoHelper.getInstance().multiInsert(list);
    }

    public static List<Friends> queryFriends() {
        friends = GreenDaoHelper.getInstance().queryWithFilter(Friends.class, FriendsDao.Properties.Uid.eq(UserInfo.UID));
        return friends;
    }
    public static List<Employee> queryEmployeeS(boolean isfrined) {
        if(isfrined){
            employees = GreenDaoHelper.getInstance().select(Employee.class);

//            for (int i= 0; i < friends.size();i++){
//                employee = (Employee) (GreenDaoHelper.getInstance().queryWithFilter(Employee.class, EmployeeDao.Properties.Uid.eq(queryFriends().get(i)))).get(0);
//                employees.add(employee);
//            }
        }
        else {
            employees = GreenDaoHelper.getInstance().queryAll(Employee.class);
        }

        return employees;
    }

    public static Employee queryAssignEmployee(String userid) {

        long uid = Long.parseLong(userid);
        employees = GreenDaoHelper.getInstance().queryWithFilter(Employee.class, EmployeeDao.Properties.Uid.eq(uid));
        if(employees.size()>0)
                employee = (employees.get(0));
        return employee;
    }
    public static Employee queryAssignEmployeeByName(String userName) {
        try {

        employees = GreenDaoHelper.getInstance().queryWithFilter(Employee.class, EmployeeDao.Properties.UserName.eq(userName));
        if(employees.size()>0)
            employee = (employees.get(0));

        }catch (Exception e){
            employee = null;
        }

        return employee;
    }
    public static List<Employee> queryWhereEmployee( boolean is) {
        employees = GreenDaoHelper.getInstance().queryWithFilter(Employee.class, EmployeeDao.Properties.IsUsual.eq(is));
        return employees;
    }
    public static List<Department> queryDepartmentS() {
        try {
            departments = GreenDaoHelper.getInstance().queryAll(Department.class);
        }catch (Exception e){
            departments = null;
        }
        return departments;
    }
    public static void upDataAssignEmployee(Employee employee) {

        GreenDaoHelper.getInstance().singleUpdate(employee);

    }



    public String Bitmap2StrByBase64(Bitmap bit) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bit.compress(Bitmap.CompressFormat.JPEG, 80, bos);//参数100表示不压缩
        byte[] bytes = bos.toByteArray();
        return Base64.encodeToString(bytes, Base64.DEFAULT);
    }

}
